package com.cskaoyan._02basic;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/2 15:52
 **/
/*
案例1: 自然数求和
1+2+3+4...n
f(10) = 10 + f(9)
f(9)  = 9 + f(8)
....

f(2) = 2+ f(1)
f(1) = 1

f(n) = n + f(n-1)

 */
public class Demo {
    public static void main(String[] args) {
        int sum = getSum(100);
        System.out.println("sum = " + sum);
    }

    public static int getSum(int n) {
        // 一定要有递归出口
        if (n == 1) {
            return 1;
        }

        // n + getSum(n - 1) 递归体
        return n + getSum(n - 1);
    }

}
